<?php 

/**
* 订单控制器
*/
class OrderController extends CommonController{
	private $model;
	/**
	 * [__init 构造函数]
	 * @return [type] [description]
	 */
	public function __init(){
		parent::__init();
		$this->model=K("Order");
	}
	/**
	 * [index 首页动作]
	 * @return [type] [description]
	 */
	public function index(){
		if (!isset($_SESSION['uid'])) {
            $this->success('未登录,请先登录!',U('Login/index'));
        }
        $uid =$_SESSION['uid'];
        // 获取用户存放在购物车里面的表
        $cartData =K('Cart')->get_one_data(array('uid'=>$uid));
        if ($cartData) {
        	$cartData =unserialize($cartData['cartinfo']);
        }else{
        	$this->success('购物车什么都没有.继续购物吧',U('Index/index'));
        }
        // 分配变量
        $this->assign('cartData',$cartData);
		// 显示视图
		$this->dis('order.html');
	}
	/**
	 * [add 增加动作]
	 */
	public function add(){
		if (!IS_POST) $this->error('非法请求',__ROOT__);
		$pay_type = Q('post.pay_type',0,'intval');
		$is_bill = Q('post.is_bill',0,'intval');
		$remark = Q('post.remark');
		$uid =intval($_SESSION['uid']);
        // 获取用户存放在购物车里面的表
        $cartData =K('Cart')->get_one_data(array('uid'=>$uid));
        $cartData =unserialize($cartData['cartinfo']);
        $userData = K('User')->get_one_data(array('uid'=>$uid));
        foreach ($cartData['goods'] as $v) {
            $pic = $v['options']['pic'];
        }
        // p($userData);
        // p($cartData);
        if ($pay_type==1) {
        	$status = '待发货'; 
        }else{
        	$status = '未付款';
        }
        $data = array(
        	"number"     =>Cart::getOrderId(),    //订单号
        	"consignee"  =>$userData['nickname'], //姓名
        	"address"    =>$userData['address'],  //地址
        	"mobile"     =>$userData['telphone'], //手机
        	"total"      =>$cartData['total'],    //总价
        	'remark'     =>$remark,               //备注
        	'uid'        =>$uid,                  //用户ID
        	'is_bill'    =>$is_bill,              //是否发票
        	'status'     =>$status,               //状态
            'pic'        =>$pic,                  //图片
            "pay_type"   =>$pay_type              //支付方式
        	);
        $oid = $this->model->add_data($data);
        foreach ($cartData['goods'] as $v) {
        	$data = array(
        		'quantity' =>$v['num'],
        		'subtotal' =>$v['total'],
        		'explain'  =>$remark,
        		'gid'      =>$v['options']['gid'],
        		'oid'      =>$oid,
        		'glid'     =>$v['id'],
        		'glnumber' =>$v['options']['number'],
                'pic'      =>$v['options']['pic']
        		);
        	$olid =K('OrderList')->add_data($data);
        }
        // 生成订单后,执行删除SESSION里面的订单信息和购物车表里面的信息
        cart::delAll();
        K('Cart')->del_data(array('uid'=>$uid));
        if ($pay_type==1) {
            $this->dis('end.html');
        }else{
        	$this->success('订单已生成,继续完成在线支付',U('Pay/index',array('oid'=>$oid)));
        }  
	}
	
}
















 ?>